package com.baibei.ebec.web;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.ebec.home.R;
import com.baibei.ebec.home.R2;
import com.baibei.ebec.sdk.TokenInfo;
import com.baibei.model.UserInfo;
import com.baibei.module.AppRouter;
import com.baibei.widget.dialog.ShareDialog;
import com.blankj.utilcode.utils.LogUtils;
import com.rae.swift.session.SessionManager;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
public class CampaignActivity extends AppCompatActivity {

    @BindView(R2.id.wv)
    WebView wv;
    private String url;
    private Map<String, String> map;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_campaign);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        init();
    }

    private void init() {
        Map<String, String> map = new HashMap<>();
        map.put("Referer","http://h5.shzstr.cn");
        wv.loadUrl(url,map);
        WebSettings settings = wv.getSettings();
        settings.setJavaScriptEnabled(true);

        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setSupportZoom(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setGeolocationEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setAllowFileAccess(true);
        settings.setSupportZoom(true);
        settings.setLoadWithOverviewMode(true);
        settings.setPluginState(WebSettings.PluginState.ON);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE); // 不加载缓存内容

        wv.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        wv.addJavascriptInterface(new JiaoHu(),"android");
//        map = new HashMap<>();
//        map.put("Referer","http://h5.shzstr.cn");
//        WebSettings settings = wv.getSettings();
//        settings.setJavaScriptEnabled(true);
//        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//        settings.setDefaultTextEncodingName("UTF-8");
//
//        settings.setDomStorageEnabled(true); //重点是这个设置
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
//        }
//
//        wv.getSettings().setDomStorageEnabled(true);
//        wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
//        wv.setWebChromeClient(new WebChromeClient());
//        WebViewClient webViewClient = new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                // 如下方案可在非微信内部WebView的H5页面中调出微信支付
//                if (url.startsWith("weixin://wap/pay?")) {
//                    Intent intent = new Intent();
//                    intent.setAction(Intent.ACTION_VIEW);
//                    intent.setData(Uri.parse(url));
//                    startActivity(intent);
//                    return true;
//                }
//
//                else {
//                    Map<String, String> extraHeaders = new HashMap<String, String>();
//                    extraHeaders.put("Referer", "http://wxpay.wxutil.com");
//                    view.loadUrl(url, extraHeaders);
//                }
//                return true;
//            }
//
//
//            // 重写此方法可以让webview处理https请求
//            @Override
//            public void onReceivedSslError(WebView view, SslErrorHandler handler, android.net.http.SslError error) {
//                handler.proceed();
//            }
//        };
//        wv.setWebViewClient(webViewClient);
//        LogUtils.e("urlsss",url);
//        wv.loadUrl(url, map);
//        wv.addJavascriptInterface(new JiaoHu(),"android");
    }

    public class JiaoHu{
        @JavascriptInterface
        public void backHome(){
            AppRouter.routeToMain(getApplicationContext(),0);
        }

        @JavascriptInterface
        public void toShare(){
            AppRouter.routeToMain(getApplicationContext(),"share");
        }
        /**
         * 获取用户信息
         */

        @JavascriptInterface
        public String getUserInfo(){
            TokenInfo userToken = SessionManager.getDefault().getUserToken();
            String accessToken = userToken.getAccessToken();
            return accessToken;
        }

        @JavascriptInterface
        public int getUserId(){
            UserInfo userInfo = SessionManager.getDefault().getUser();
            return userInfo.getUserId();
        }

        @JavascriptInterface
        public void toQuan(){
            AppRouter.routeToTicket(getApplicationContext());
        }

        @JavascriptInterface
        public void toLogin(){
            AppRouter.routeToLogin(CampaignActivity.this);
        }

        @JavascriptInterface
        public void toChongZhi(){
            AppRouter.routeToRecharge(getApplicationContext());
        }
    }
}

